﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetroFramework.Forms;
using System.IO.Ports;
using System.IO;

namespace CVR100AReader
{
    public partial class FormSetting : MetroForm
    {
        private IDCReaderOption _option = new IDCReaderOption();
        public IDCReaderOption Option
        {
            get { return _option; }
            set { _option = value; }
        }

        public FormSetting()
        {            
            InitializeComponent();
            LoadConfig();
        }

        private void LoadConfig()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            _option.Load();
        }


        private void InitCtrls()
        {
            metroComboBoxComNo.Items.Clear();
            string[] names = SerialPort.GetPortNames();
            metroComboBoxComNo.Items.AddRange(names);
            for (int i = 0; i < metroComboBoxEncoding.Items.Count; i++)
            {
                if (_option.EncodingName.Equals(metroComboBoxEncoding.Items[i].ToString()))
                {
                    metroComboBoxEncoding.SelectedIndex = i;
                }
            }
            for (int i = 0; i < metroComboBoxBaudrate.Items.Count; i++)
            {
                string item = metroComboBoxBaudrate.Items[i].ToString();
                if(item.Equals(_option.Baudrate.ToString()))
                {
                    metroComboBoxBaudrate.SelectedIndex= i;
                }
            }
            metroToggleReadHeadImage.Checked = _option.ReadHeadImage;
            metroToggleReadFingerprint.Checked = _option.ReadFingerprint;
            intRangeTrackBarScanInterval.Value = _option.ScanInterval;
            intRangeTrackBarTimeout.Value = _option.TimeoutSecond;

            string defaultComNo = "COM" + _option.SerialPortNum.ToString();
            for (int i = 0; i < metroComboBoxComNo.Items.Count; i++)
            {
                if (defaultComNo.Equals(metroComboBoxComNo.Items[i].ToString()))
                {
                    metroComboBoxComNo.SelectedIndex = i;
                }
            }
        }

        private bool UpdateData()
        {
            if (metroComboBoxBaudrate.SelectedItem == null)
            {
                MessageBox.Show("请选择通信波特率");
                return false;
            }
            if (metroComboBoxComNo.SelectedItem == null)
            {
                MessageBox.Show("没有选择任何一个COM口");
                return false;
            }
            string comNo = metroComboBoxComNo.SelectedItem.ToString();
            if (!comNo.Contains("COM"))
            {
                MessageBox.Show("COM口选择错误:" + comNo);
                return false;
            }
            int number = Convert.ToInt32(comNo.Substring(3, comNo.Length - 3));
            _option.SerialPortNum = number;
            _option.Baudrate = int.Parse(metroComboBoxBaudrate.SelectedItem.ToString());
            _option.EncodingName = metroComboBoxEncoding.SelectedItem.ToString();
            _option.ReadHeadImage = metroToggleReadHeadImage.Checked;
            _option.ReadFingerprint = metroToggleReadFingerprint.Checked;
            _option.ScanInterval = intRangeTrackBarScanInterval.Value;
            _option.TimeoutSecond = intRangeTrackBarTimeout.Value;
            return _option.Save();
        }

    
        private void buttonCancell_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (UpdateData())
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }           
        }

        private void FormIDCReaderSetting_Load(object sender, EventArgs e)
        {
            InitCtrls();
        }
    }
}
